package com.global.security.enviroment;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.*;

/**
 * Sercurity environment post processor
 *
 * @description:
 * @author: cyj
 * @date 2022 -05-26 17:21:41
 *
 *
 */
@Slf4j
public class SercurityEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

    @SneakyThrows
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

//        MutablePropertySources propertySources = environment.getPropertySources();
//        Iterator<PropertySource<?>> iterator = propertySources.iterator();
//
//        List<Class> enviromentSecurityClasses = ClassUtil.getAllInterfaceAchieveClass(EnviromentSecurity.class);
//        EnviromentSecurity enviromentSecurity = null;
//
//        for (Class enviromentSecurityClass:
//             enviromentSecurityClasses) {
//            EnviromentSecurity enviromentSecurityTemp = (EnviromentSecurity) enviromentSecurityClass.newInstance();
//            if (null == enviromentSecurity) {
//                enviromentSecurity = enviromentSecurityTemp;
//            } else if (enviromentSecurity.getOrder() < enviromentSecurityTemp.getOrder()) {
//                enviromentSecurity = enviromentSecurityTemp;
//            }
//        }
//
//        Map<String, Object> sercurityProperty = new HashMap<>();
//
//        if (null != enviromentSecurity) {
//            while (iterator.hasNext()) {
//                PropertySource<?> propertySource = iterator.next();
//                Object source = propertySource.getSource();
//                if (source instanceof Map) {
//                    Map<String, Object> sourceMap = (Map<String, Object>) source;
//                    for (String key:
//                    sourceMap.keySet()) {
//                        String value = String.valueOf(sourceMap.get(key));
//                        if (value.startsWith(EnviromentSecurity.ENCRYPT_PRFIX)) {
//                            String encrypt = enviromentSecurity.encrypt(value);
//                            sercurityProperty.put(key, encrypt);
//                        } else if (value.startsWith(EnviromentSecurity.DECRYPT_PRFIX)) {
//                            String decrypt = enviromentSecurity.decrypt(value);
//                            sercurityProperty.put(key, decrypt);
//                        }
//                    }
//                }
//            }
//        }
//
//        /**
//         * 将解密的数据放入环境变量，并处于第一优先级上
//         * (这里一定要注意，覆盖其他配置)
//         */
//        if (!sercurityProperty.isEmpty()) {
//            System.out.println("修改env");
//            environment.getPropertySources().addFirst(new MapPropertySource("sercurityProperty", sercurityProperty));
//        }
    }

    @Override
    public int getOrder() {
        return  ConfigFileApplicationListener.DEFAULT_ORDER + 1;
    }
}
